package cn.joybreeze.controller;

import cn.joybreeze.common.result.R;
import cn.joybreeze.common.vo.UserVO;
import cn.joybreeze.service.impl.UmsAdminServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName UmsAdminController
 * @Description
 * @Author Kjwaa
 * @Email 1135274290@qq.com
 * @Date 2021/5/13 22:06
 */
@RestController
@RequestMapping("/admin")
public class UmsAdminController {
    @Autowired
    UmsAdminServiceImpl umsAdminService;


    @PostMapping("/login")
    public R login(@RequestBody  UserVO userVO){
        // 前端直接穿username password过来好像不太行，要封装成一个请求体才行
        String token = umsAdminService.login(userVO.getUsername(), userVO.getPassword());
        HashMap<String, String> data = new HashMap<>();
        if (token == null) {
            return R.error("用户名或密码错误");
        }
        data.put("token", token);
        return R.ok(data);
    }

    @PostMapping("/register")
    public R register(@RequestBody UserVO userVO){
        // 又是不加@RequestBody就无法获取对应参数
        Boolean isSuccess = umsAdminService.register(userVO);
        if(isSuccess) return R.ok();
        return R.error();
    }

}
